const myReq = function (option) {
  return new Promise((res, rej) => {
    if (!option.success) {
      option.success = response => {
        if (response.data.code !== 2000) {
          if (response.data.code === 3000) {
            new Promise((res, rej) => {
                wx.getStorage({
                  key: 'openid',
                  success(response) {
                    res(response.data)
                  }
                })
              }).then(
                response => {
                  return new Promise((res, rej) => {
                    wx.request({
                      url: 'http://api_dev.wanxikeji.cn/api/refreshToken',
                      method: "POST",
                      data: {
                        openid: response
                      },
                      success(response) {
                        if (response.data.code === 2010) {
                          rej({
                            "code": 2010,
                            "data": [],
                            "msg": "用户已被禁用"
                          })
                          return
                        }
                        wx.setStorage({
                          data: response.data.data.token,
                          key: 'token',
                        })
                        res(response.data.data.token)
                      }
                    })
                  })
                }
              )
              .then(
                response => {
                  option.data.token = response
                  myReq(option)
                }
              )
          } else {
            rej("服务器错误,但不是token出错"+JSON.stringify(response))
          }

        } else if (response.data.code === 2000) {
          res(response)
        }
      }
    }
    wx.request(option)
  })
}
module.exports = myReq